package cn.aishua.ui.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import cn.aishua.ui.main.MainBigActivity.ListNavSearchPromptAdapter.ListNavSearchPromptAdapterDataItem;

import cn.aishua.R;

public class MainBigActivity extends Activity{
	
	LinearLayout l1;
	LinearLayout l2;
	LinearLayout l3;
	EditText editInvisible;
	EditText editNavSearch;
	ImageView imageNavInputDenial;
	ListView listNavSearchPrompt;
	LinearLayout btnNavHotones;
	LinearLayout btnNavFind;
	LinearLayout btnNavRandomFind;
	LinearLayout btnNavGetAll;
	LinearLayout btnNavSubFood;
	LinearLayout btnNavSubEntertain;
	LinearLayout btnNavSubLife;
	LinearLayout btnNavSubTravel;
	LinearLayout btnNavSubShop;
	
	ImageView imageMainHeaderShowNav;
	TextView textMainHeaderTitle;
	ImageView imageMainHeaderFunc;
	
	LinearLayout linearPicMode;
	LinearLayout linearWallpaperMode;
	ImageView imagePicsVendor1;
	ImageView imagePicsVendor2;
	ImageView imagePicsVendor3;
	ImageView imagePicsVendor4;
	ImageView imagePicsVendor5;
	ImageView imagePicsVendor6;
	ImageView imagePicsVendor7;
	ImageView imagePicsClass;
	
	ImageView imageWallpaper;
	
	LinearLayout linearFind;
	TextView textFindChooseArea;
	TextView editFindChooseArea;
	TextView textFindVendorClass;
	TextView editFindVendorClass;
	TextView textFindAvgSpend;
	
	LinearLayout linearFindAvgSpending;
	View btnSpendRangeLeft;
	View btnSpendRangeRight;
	View btnSpendRangeMiddle;
	TextView textSpendRangeLeft;
	TextView textSpendRangeRight;
	
	TextView textFindRebate;
	TextView textFindRebateClassAll;
	TextView textFindRebateClassRate05;
	TextView textFindRebateClassRate610;
	TextView textFindRebateClassRate1120;
	TextView textFindRebateClassRate21;
	
	Button btnFindOk;
	
	LinearLayout linearRandomFind;
	TextView textRandomFindMsg;
	LinearLayout linearRandomFindCenter;
	ImageView imageRandomFind;
	LinearLayout linearRandomFindBtnGroup;
	TextView textRandomFindChooseVendor;
	TextView textRandomFindChooseRebateRate;
	TextView textRandomFindChooseAvgSpend;
	
	LinearLayout linearGetAllList;
	
	
	LinearLayout linearGetAllMap;
	
	
	
	LinearLayout linearLayout4;
	TextView textAreaClassArea;
	TextView textAreaClassAllArea;
	TextView textAreaClassWuhou;
	TextView textAreaClassQingyang;
	TextView textAreaClassChenghua;
	TextView textAreaClassJinniu;
	TextView textAreaClassJinjiang;
	TextView textAreaClassGaoxin;
	TextView textAreaClassJinjiao;
	
	TextView textAreaItem1;
	TextView textAreaItem2;
	TextView textAreaItem3;
	TextView textAreaItem4;
	
	int whoCalledVendorChoose = -1; //0(Find), 1(Random Find), 2
	LinearLayout linearLayout5;
	TextView textVendorClassAll;
	TextView textVendorClassFood;
	TextView textVendorClassEntertain;
	TextView textVendorClassLife;
	TextView textVendorClassTravel;
	TextView textVendorClassShop;
	
	TextView textVendorItem1;
	TextView textVendorItem2;
	TextView textVendorItem3;
	TextView textVendorItem4;
	TextView textVendorItem5;
	

	
	
	int WIDTH;
	ListAdapter listNavSearchPromptAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_ui);
		init();

	}
	
	
	OnKeyListener editNavSearchKeyListener = new OnKeyListener() {
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if(keyCode == KeyEvent.KEYCODE_ENTER){
				//TODO start to search (remember to hide prompt 
				InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			    imm.hideSoftInputFromWindow(editNavSearch.getWindowToken(), 0);
			    editInvisible.requestFocus();
			    l3.setVisibility(View.GONE);
			    
			}
			return false;
		}
	};
	
	OnFocusChangeListener editNavSearchFocusChangeListener= new OnFocusChangeListener(){
		public void onFocusChange(View v, boolean hasFocus) {
			Log.d("test", "onFocusChange " + hasFocus + ", v " + v);
			if(hasFocus){
				l3.setVisibility(View.VISIBLE);
			}else{
				l3.setVisibility(View.GONE);
			}
		}
	};
	
	TextWatcher editNavSearchTextWatcher = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if(s.length() != 0 && imageNavInputDenial.getVisibility() == View.INVISIBLE){
				imageNavInputDenial.setVisibility(View.VISIBLE);
			}
			if(s.length() == 0 && imageNavInputDenial.getVisibility() == View.VISIBLE){
				imageNavInputDenial.setVisibility(View.INVISIBLE);
			}
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		public void afterTextChanged(Editable s) {
		}
	};
	
	View.OnClickListener imageNavInputClearClickListener = new OnClickListener() {
		public void onClick(View v) {
			editNavSearch.setText("");
			editNavSearch.requestFocus();
		}
	};
	
	OnClickListener btnNavHotonesClickListener = new OnClickListener() {
		public void onClick(View v) {
			hideAll();
			linearPicMode.setVisibility(View.VISIBLE);
			imageMainHeaderFunc.setImageResource(R.drawable.ic_header_wallpaper_mode);
			WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_PICS_MODE;
			imageMainHeaderShowNav.performClick();
			textMainHeaderTitle.setText("热门推荐");
		}
	};
	
	OnClickListener btnNavFindClickListener = new OnClickListener() {
		public void onClick(View v) {
			hideAll();
			linearFind.setVisibility(View.VISIBLE);
			imageMainHeaderFunc.setImageResource(R.drawable.ic_header_random_find);
			WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_FIND;
			imageMainHeaderShowNav.performClick();
			textMainHeaderTitle.setText("查找商户");
		}
	};
	
	OnClickListener btnNavRandomFindClickListener = new OnClickListener() {
		public void onClick(View v) {
			hideAll();
			linearRandomFind.setVisibility(View.VISIBLE);
			imageMainHeaderFunc.setImageResource(R.drawable.ic_header_find);
			WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_RANDOM_FIND;
			imageMainHeaderShowNav.performClick();
			textMainHeaderTitle.setText("摇一摇");
		}
	};
	
	OnClickListener btnNavGetAllClickListener = new OnClickListener() {
		public void onClick(View v) {
			hideAll();
			linearGetAllList.setVisibility(View.VISIBLE);
			imageMainHeaderFunc.setImageResource(R.drawable.ic_header_all_map);
			WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_GET_ALL;
			imageMainHeaderShowNav.performClick();
			textMainHeaderTitle.setText("全部商户");
		}
	};
	
	OnClickListener btnNavSubFoodClickListener = new OnClickListener() {
		public void onClick(View v) {
			l("clicked");
		}
	};
	
	OnClickListener btnNavSubEntertainClickListener = new OnClickListener() {
		public void onClick(View v) {
			l("clicked");
		}
	};
	
	OnClickListener btnNavSubLifeClickListener = new OnClickListener() {
		public void onClick(View v) {
			l("clicked");
		}
	};
	
	OnClickListener btnNavSubTravelClickListener = new OnClickListener() {
		public void onClick(View v) {
			l("clicked");
		}
	};
	
	OnClickListener btnNavSubShopClickListener = new OnClickListener() {
		public void onClick(View v) {
			l("clicked");
		}
	};
	
	OnClickListener editFindChooseAreaClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			linearLayout4.setVisibility(View.VISIBLE);
			switchViewsForFind(false);
		}
	};
	
	OnClickListener editFindVendorClassClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			whoCalledVendorChoose = 0;
			linearLayout5.setVisibility(View.VISIBLE);
			switchViewsForFind(false);
		}
	};
	
	private void switchViewsForFind(boolean what){
		imageMainHeaderShowNav.setEnabled(what);
		imageMainHeaderFunc.setEnabled(what);
		textMainHeaderTitle.setEnabled(what);
		textFindChooseArea.setEnabled(what);
		editFindChooseArea.setEnabled(what);
		textFindVendorClass.setEnabled(what);
		editFindVendorClass.setEnabled(what);
		textFindAvgSpend.setEnabled(what);
	}
	
	private void switchViewsForRandomFind(boolean what){
		textRandomFindMsg.setEnabled(what);
		imageRandomFind.setEnabled(what);
		textRandomFindChooseVendor.setEnabled(what);
		textRandomFindChooseRebateRate.setEnabled(what);
		textRandomFindChooseAvgSpend.setEnabled(what);
	}
	
	private void init(){
		editInvisible = (EditText)findViewById(R.id.editInvisible);
		l1 = (LinearLayout)findViewById(R.id.linearLayout1);
		l2 = (LinearLayout)findViewById(R.id.linearLayout2);
		l3 = (LinearLayout)findViewById(R.id.linearLayout3);
		l3.setVisibility(View.GONE);
		WIDTH = l1.getLayoutParams().width;
		
		//--- find views and initialize, subscribe event handler
		editNavSearch = (EditText)findViewById(R.id.editNavSearch);
		editNavSearch.addTextChangedListener(editNavSearchTextWatcher);
		editNavSearch.setOnKeyListener(editNavSearchKeyListener);
		editNavSearch.setOnFocusChangeListener(editNavSearchFocusChangeListener);
		
		imageNavInputDenial = (ImageView)findViewById(R.id.imageNavInputDenial);
		imageNavInputDenial.setVisibility(View.INVISIBLE);
		imageNavInputDenial.setClickable(true);
		imageNavInputDenial.setOnClickListener(imageNavInputClearClickListener);
		
		listNavSearchPrompt = (ListView)findViewById(R.id.listNavSearchPrompt);
		ListNavSearchPromptAdapterDataItem[] data = new ListNavSearchPromptAdapterDataItem[]{
				new ListNavSearchPromptAdapterDataItem("皇城火锅", true),
				new ListNavSearchPromptAdapterDataItem("滋滋烤鱼", true),
				new ListNavSearchPromptAdapterDataItem("胖哥小吃", true),
				new ListNavSearchPromptAdapterDataItem("狗不理包子", true),
				new ListNavSearchPromptAdapterDataItem("宜宾燃面", true)
		};
		listNavSearchPromptAdapter = new ListNavSearchPromptAdapter(this, data);
		listNavSearchPrompt.setAdapter(listNavSearchPromptAdapter);
		
		btnNavHotones = (LinearLayout)findViewById(R.id.btnNavHotones);
		btnNavHotones.setOnClickListener(btnNavHotonesClickListener);
		
		btnNavFind = (LinearLayout)findViewById(R.id.btnNavFind);
		btnNavFind.setOnClickListener(btnNavFindClickListener);
		
		btnNavRandomFind = (LinearLayout)findViewById(R.id.btnNavRandomFind);
		btnNavRandomFind.setOnClickListener(btnNavRandomFindClickListener);
		
		btnNavGetAll = (LinearLayout)findViewById(R.id.btnNavGetAll);
		btnNavGetAll.setOnClickListener(btnNavGetAllClickListener);
		
		btnNavSubFood = (LinearLayout)findViewById(R.id.btnNavSubFood);
		btnNavSubFood.setOnClickListener(btnNavSubFoodClickListener);
		
		btnNavSubEntertain = (LinearLayout)findViewById(R.id.btnNavSubEntertain);
		btnNavSubEntertain.setOnClickListener(btnNavSubEntertainClickListener);
		
		btnNavSubLife = (LinearLayout)findViewById(R.id.btnNavSubLife);
		btnNavSubLife.setOnClickListener(btnNavSubLifeClickListener);
		
		btnNavSubTravel = (LinearLayout)findViewById(R.id.btnNavSubTravel);
		btnNavSubTravel.setOnClickListener(btnNavSubTravelClickListener);
		
		btnNavSubShop = (LinearLayout)findViewById(R.id.btnNavSubShop);
		btnNavSubShop.setOnClickListener(btnNavSubShopClickListener);
		
		imageMainHeaderShowNav = (ImageView)findViewById(R.id.imageMainHeaderShowNav);
		imageMainHeaderShowNav.setOnClickListener(imageMainHeaderShowNavClickListener);
		
		imageMainHeaderFunc = (ImageView)findViewById(R.id.imageMainHeaderFunc);
		imageMainHeaderFunc.setOnClickListener(imageMainHeaderFuncClickListener);
		
		textMainHeaderTitle = (TextView)findViewById(R.id.textMainHeaderTitle);
		
		linearPicMode = (LinearLayout)findViewById(R.id.linearPicMode);
		imagePicsVendor1 = (ImageView)findViewById(R.id.imagePicsVendor1);
		imagePicsVendor2 = (ImageView)findViewById(R.id.imagePicsVendor2);
		imagePicsVendor3 = (ImageView)findViewById(R.id.imagePicsVendor3);
		imagePicsVendor4 = (ImageView)findViewById(R.id.imagePicsVendor4);
		imagePicsVendor5 = (ImageView)findViewById(R.id.imagePicsVendor5);
		imagePicsVendor6 = (ImageView)findViewById(R.id.imagePicsVendor6);
		imagePicsVendor7 = (ImageView)findViewById(R.id.imagePicsVendor7);
		imagePicsClass = (ImageView)findViewById(R.id.imagePicsClass);
		
		//---here comes other modules
		linearWallpaperMode = (LinearLayout)findViewById(R.id.linearWallpaperMode);
		
		imagePicsVendor1.setOnClickListener(dummyClickListener);
		imagePicsVendor2.setOnClickListener(dummyClickListener);
		imagePicsVendor3.setOnClickListener(dummyClickListener);
		imagePicsVendor4.setOnClickListener(dummyClickListener);
		imagePicsVendor5.setOnClickListener(dummyClickListener);
		imagePicsVendor6.setOnClickListener(dummyClickListener);
		imagePicsVendor7.setOnClickListener(dummyClickListener);
		imagePicsClass.setOnClickListener(dummyClickListener);
		
		imagePicsVendor1.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor2.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor3.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor4.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor5.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor6.setOnTouchListener(mainCommonTouchListener);
		imagePicsVendor7.setOnTouchListener(mainCommonTouchListener);
		imagePicsClass.setOnTouchListener(mainCommonTouchListener);
		
		imageWallpaper = (ImageView)findViewById(R.id.imageWallpaper);
		imageWallpaper.setOnTouchListener(mainCommonTouchListener);
		imageWallpaper.setOnClickListener(dummyClickListener);
		
		linearFind = (LinearLayout)findViewById(R.id.linearFind);
		textFindChooseArea = (TextView)findViewById(R.id.textFindChooseArea);
		editFindChooseArea = (TextView)findViewById(R.id.editFindChooseArea);
		textFindVendorClass = (TextView)findViewById(R.id.textFindVendorClass);
		editFindVendorClass = (TextView)findViewById(R.id.editFindVendorClass);
		textFindAvgSpend = (TextView)findViewById(R.id.textFindAvgSpend);
		editFindChooseArea.setText("全部区域");
		editFindVendorClass.setText("全部商户");
		textFindChooseArea.setOnTouchListener(mainCommonTouchListener);
		editFindChooseArea.setOnTouchListener(mainCommonTouchListener);
		textFindVendorClass.setOnTouchListener(mainCommonTouchListener);
		editFindVendorClass.setOnTouchListener(mainCommonTouchListener);
		textFindAvgSpend.setOnTouchListener(mainCommonTouchListener);
		textFindChooseArea.setOnClickListener(dummyClickListener);
		editFindChooseArea.setOnClickListener(editFindChooseAreaClickListener);
		textFindVendorClass.setOnClickListener(dummyClickListener);
		editFindVendorClass.setOnClickListener(editFindVendorClassClickListener);
		textFindAvgSpend.setOnClickListener(dummyClickListener);
        textFindRebate = (TextView)findViewById(R.id.textFindRebate);
        textFindRebate.setOnTouchListener(mainCommonTouchListener);
        textFindRebate.setOnClickListener(dummyClickListener);
		
		linearLayout4 = (LinearLayout)findViewById(R.id.linearLayout4);
		linearLayout4.setVisibility(View.GONE);
		
		textAreaClassArea = (TextView)findViewById(R.id.textAreaClassArea);
		textAreaClassAllArea = (TextView)findViewById(R.id.textAreaClassAllArea);
		textAreaClassWuhou = (TextView)findViewById(R.id.textAreaClassWuhou);
		textAreaClassQingyang = (TextView)findViewById(R.id.textAreaClassQingyang);
		textAreaClassChenghua = (TextView)findViewById(R.id.textAreaClassChenghua);
		textAreaClassJinniu = (TextView)findViewById(R.id.textAreaClassJinniu);
		textAreaClassJinjiang = (TextView)findViewById(R.id.textAreaClassJinjiang);
		textAreaClassGaoxin = (TextView)findViewById(R.id.textAreaClassGaoxin);
		textAreaClassJinjiao = (TextView)findViewById(R.id.textAreaClassJinjiao);
		
		textAreaItem1 = (TextView)findViewById(R.id.textAreaItem1);
		textAreaItem2 = (TextView)findViewById(R.id.textAreaItem2);
		textAreaItem3 = (TextView)findViewById(R.id.textAreaItem3);
		textAreaItem4 = (TextView)findViewById(R.id.textAreaItem4);
		
		textAreaClassArea.setOnClickListener(textAreaClassAreaClickListener);
		textAreaClassAllArea.setOnClickListener(textAreaClassAllAreaClickListener);
		textAreaClassWuhou.setOnClickListener(textAreaClassWuhouClickListener);
		textAreaClassQingyang.setOnClickListener(textAreaClassQingyangClickListener);
		textAreaClassChenghua.setOnClickListener(textAreaClassChenghuaClickListener);
		textAreaClassJinniu.setOnClickListener(textAreaClassJinniuClickListener);
		textAreaClassJinjiang.setOnClickListener(textAreaClassJinjiangClickListener);
		textAreaClassGaoxin.setOnClickListener(textAreaClassGaoxinClickListener);
		textAreaClassJinjiao.setOnClickListener(textAreaClassJinjiaoClickListener);
		
		textAreaItem1.setOnClickListener(textAreaItem1ClickListener);
		textAreaItem2.setOnClickListener(textAreaItem2ClickListener);
		textAreaItem3.setOnClickListener(textAreaItem3ClickListener);
		textAreaItem4.setOnClickListener(textAreaItem4ClickListener);
		
		textAreaClassAllArea.performClick();
		
		linearLayout5 = (LinearLayout)findViewById(R.id.linearLayout5);
		linearLayout5.setVisibility(View.GONE);
		
		textVendorClassAll = (TextView)findViewById(R.id.textVendorClassAll);
		textVendorClassFood = (TextView)findViewById(R.id.textVendorClassFood);
		textVendorClassEntertain = (TextView)findViewById(R.id.textVendorClassEntertain);
		textVendorClassLife = (TextView)findViewById(R.id.textVendorClassLife);
		textVendorClassTravel = (TextView)findViewById(R.id.textVendorClassTravel);
		textVendorClassShop = (TextView)findViewById(R.id.textVendorClassShop);
		
		textVendorItem1 = (TextView)findViewById(R.id.textVendorItem1);
		textVendorItem2 = (TextView)findViewById(R.id.textVendorItem2);
		textVendorItem3 = (TextView)findViewById(R.id.textVendorItem3);
		textVendorItem4 = (TextView)findViewById(R.id.textVendorItem4);
		textVendorItem5 = (TextView)findViewById(R.id.textVendorItem5);
		
		textVendorClassAll.setOnClickListener(textVendorClassAllClickListener);
		textVendorClassFood.setOnClickListener(textVendorClassFoodClickListener);
		textVendorClassEntertain.setOnClickListener(textVendorClassEntertainClickListener);
		textVendorClassLife.setOnClickListener(textVendorClassLifeClickListener);
		textVendorClassTravel.setOnClickListener(textVendorClassTravelClickListener);
		textVendorClassShop.setOnClickListener(textVendorClassShopClickListener);
		
		textVendorItem1.setOnClickListener(textVendorItem1ClickListener);
		textVendorItem2.setOnClickListener(textVendorItem2ClickListener);
		textVendorItem3.setOnClickListener(textVendorItem3ClickListener);
		textVendorItem4.setOnClickListener(textVendorItem4ClickListener);
		textVendorItem5.setOnClickListener(textVendorItem5ClickListener);
		
		textVendorClassAll.performClick();
		
        btnSpendRangeLeft = (View)findViewById(R.id.btnSpendRangeLeft);
        btnSpendRangeRight = (View)findViewById(R.id.btnSpendRangeRight);
        btnSpendRangeMiddle = (View)findViewById(R.id.btnSpendRangeMiddle);
        textSpendRangeLeft = (TextView)findViewById(R.id.textSpendRangeLeft);
        textSpendRangeRight = (TextView)findViewById(R.id.textSpendRangeRight);
        
        btnSpendRangeLeft.setOnTouchListener(btnSpendRangeLeftTouchListener);
        btnSpendRangeRight.setOnTouchListener(btnSpendRangeRightTouchListener);
        linearFindAvgSpending = (LinearLayout)findViewById(R.id.linearFindAvgSpending);
        linearFindAvgSpending.setOnTouchListener(mainCommonTouchListener);
        linearFindAvgSpending.setOnClickListener(dummyClickListener);
        
        textFindRebateClassAll = (TextView)findViewById(R.id.textFindRebateClassAll);
        textFindRebateClassRate05 = (TextView)findViewById(R.id.textFindRebateClassRate05);
        textFindRebateClassRate610 = (TextView)findViewById(R.id.textFindRebateClassRate610);
        textFindRebateClassRate1120 = (TextView)findViewById(R.id.textFindRebateClassRate1120);
        textFindRebateClassRate21 = (TextView)findViewById(R.id.textFindRebateClassRate21);
        
        textFindRebateClassAll.setOnTouchListener(mainCommonTouchListener);
        textFindRebateClassRate05.setOnTouchListener(mainCommonTouchListener);
        textFindRebateClassRate610.setOnTouchListener(mainCommonTouchListener);
        textFindRebateClassRate1120.setOnTouchListener(mainCommonTouchListener);
        textFindRebateClassRate21.setOnTouchListener(mainCommonTouchListener);
        
        textFindRebateClassAll.setOnClickListener(textFindRebateClassAllClickListener);
        textFindRebateClassRate05.setOnClickListener(textFindRebateClassRate05ClickListener);
        textFindRebateClassRate610.setOnClickListener(textFindRebateClassRate610ClickListener);
        textFindRebateClassRate1120.setOnClickListener(textFindRebateClassRate1120ClickListener);
        textFindRebateClassRate21.setOnClickListener(textFindRebateClassRate21ClickListener);
        
        textFindRebateClassAll.performClick();
        
        btnFindOk = (Button)findViewById(R.id.btnFindOk);
        btnFindOk.setOnClickListener(btnFindOkClickListener);
        btnFindOk.setOnTouchListener(mainCommonTouchListener);
        
        
		linearRandomFind = (LinearLayout)findViewById(R.id.linearRandomFind);
		textRandomFindMsg = (TextView)findViewById(R.id.textRandomFindMsg);
		textRandomFindMsg.setOnTouchListener(mainCommonTouchListener);
		textRandomFindMsg.setOnClickListener(dummyClickListener);
		linearRandomFindCenter = (LinearLayout)findViewById(R.id.linearRandomFindCenter);
		makeItRight(linearRandomFindCenter);
		
		imageRandomFind = (ImageView)findViewById(R.id.imageRandomFind);

		textRandomFindChooseVendor = (TextView)findViewById(R.id.textRandomFindChooseVendor);
		textRandomFindChooseRebateRate = (TextView)findViewById(R.id.textRandomFindChooseRebateRate);
		textRandomFindChooseAvgSpend = (TextView)findViewById(R.id.textRandomFindChooseAvgSpend);
		linearRandomFindBtnGroup = (LinearLayout)findViewById(R.id.linearRandomFindBtnGroup);
		
		textRandomFindChooseVendor.setOnTouchListener(mainCommonTouchListener);
		textRandomFindChooseRebateRate.setOnTouchListener(mainCommonTouchListener);
		textRandomFindChooseAvgSpend.setOnTouchListener(mainCommonTouchListener);
		textRandomFindChooseVendor.setOnClickListener(textRandomFindChooseVendorClickListener);
		textRandomFindChooseRebateRate.setOnClickListener(textRandomFindChooseRebateRateClickListener);
		textRandomFindChooseAvgSpend.setOnClickListener(textRandomFindChooseAvgSpendClickListener);
		
		linearGetAllList = (LinearLayout)findViewById(R.id.linearGetAllList);
		
		
		
		linearGetAllMap = (LinearLayout)findViewById(R.id.linearGetAllMap);
		
		hideAll();
		linearPicMode.setVisibility(View.VISIBLE);
		switchNavViews(false);
	}
	
	OnClickListener btnFindOkClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
		}
	};
	
	OnClickListener textRandomFindChooseVendorClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			linearLayout5.setVisibility(View.VISIBLE);
			whoCalledVendorChoose = 1;
			switchViewsForRandomFind(false);
		}
	};
	
	OnClickListener textRandomFindChooseRebateRateClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			new AlertDialog.Builder(MainBigActivity.this)
								.setItems(new String[]{
										"不限返利幅度",
										"返利0-5%",
										"返利6-10%",
										"返利11-20%",
										"返利20%以上"}, new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int which) {
										dialog.dismiss();
										String text = "";
										switch(which){
											case 0:
												text = "不限返利幅度";
												break;
											case 1:
												text = "返利0-5%";
												break;
											case 2:
												text = "返利6-10%";
												break;
											case 3:
												text = "返利11-20%";
												break;
											case 4:
												text = "返利20%以上";
												break;
										}
										textRandomFindChooseRebateRate.setText(text);
									}
								})
								.show();
		}
	};
	
	OnClickListener textRandomFindChooseAvgSpendClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			new AlertDialog.Builder(MainBigActivity.this)
			.setItems(new String[]{
					"不限人均消费",
					"0-99元",
					"100-199元",
					"200-299元",
					"300元以上"}, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					String text = "";
					switch(which){
						case 0:
							text = "不限人均消费";
							break;
						case 1:
							text = "0-99元";
							break;
						case 2:
							text = "100-199元";
							break;
						case 3:
							text = "200-299元";
							break;
						case 4:
							text = "300元以上";
							break;
					}
					textRandomFindChooseAvgSpend.setText(text);
				}
			})
			.show();

		}
	};
	
	//------------------Avg spending begin
    int initX = 0;
    int moveX = 0;
    OnTouchListener btnSpendRangeLeftTouchListener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				initX = (int) event.getRawX();
				return true;
			}
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				moveX = (int) event.getRawX() - initX;
				initX = (int) event.getRawX();
				int x = ((AbsoluteLayout.LayoutParams)btnSpendRangeLeft.getLayoutParams()).x + moveX;
				
				if(x <= 0)x = 0;
				int maxX = - 50 + ((AbsoluteLayout.LayoutParams)btnSpendRangeRight.getLayoutParams()).x;
				if(x >= maxX) x = maxX;
				
				btnSpendRangeLeft.setLayoutParams(
						new AbsoluteLayout.LayoutParams(50, -1, x, 0));
				computerBtnSpendRangeMiddle();
				
				textSpendRangeLeft.setLayoutParams(
						new AbsoluteLayout.LayoutParams(75, 40, x + 5, 10));
				textSpendRangeLeft.setText("￥" + x);
				return true;
			}
			
			return true;
		}
	};
	
	private void computerBtnSpendRangeMiddle(){
		int x = ((AbsoluteLayout.LayoutParams)btnSpendRangeLeft.getLayoutParams()).x + 50;
		int width = ((AbsoluteLayout.LayoutParams)btnSpendRangeRight.getLayoutParams()).x - x;
		btnSpendRangeMiddle.setLayoutParams(
				new AbsoluteLayout.LayoutParams(width, -1, x, 0));
	}
    
    OnTouchListener btnSpendRangeRightTouchListener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				initX = (int) event.getRawX();
				return true;
			}
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				moveX = (int) event.getRawX() - initX;
				initX = (int) event.getRawX();
				int x = ((AbsoluteLayout.LayoutParams)btnSpendRangeRight.getLayoutParams()).x + moveX;
				
				if(x >= 320)x = 320;
				int minX = 50 + ((AbsoluteLayout.LayoutParams)btnSpendRangeLeft.getLayoutParams()).x;
				if(x <= minX) x = minX;
				
				btnSpendRangeRight.setLayoutParams(
						new AbsoluteLayout.LayoutParams(50, -1, x, 0));
				computerBtnSpendRangeMiddle();
				
				textSpendRangeRight.setLayoutParams(
						new AbsoluteLayout.LayoutParams(75, 40, x + 35, 10));
				int upPrice = (x + 50);
				String text = "";
				if(upPrice < 300)
					text = "￥" + upPrice;
				else if(upPrice >= 300 && upPrice <= 350)
					text = "￥" + 300;
				else
					text = "不限";
				textSpendRangeRight.setText(text);
				return true;
			}
			
			return true;
		}
	};
	
	//------------------Avg spending end
	
	int findRebateRateIndex = -1;
	OnClickListener textFindRebateClassAllClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick || findRebateRateIndex == 0)
				return;
			findRebateRateIndex = 0;
			unclickFindRebateRateAll();
			textFindRebateClassAll.setBackgroundColor(0xb8b8b8);
		}
	};
	
	OnClickListener textFindRebateClassRate05ClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick || findRebateRateIndex == 1)
				return;
			findRebateRateIndex = 1;
			unclickFindRebateRateAll();
			textFindRebateClassRate05.setBackgroundColor(0xb8b8b8);
		}
	};
	
	OnClickListener textFindRebateClassRate610ClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick || findRebateRateIndex == 2)
				return;
			findRebateRateIndex = 2;
			unclickFindRebateRateAll();
			textFindRebateClassRate610.setBackgroundColor(0xb8b8b8);
		}
	};
	
	OnClickListener textFindRebateClassRate1120ClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick || findRebateRateIndex == 3)
				return;
			findRebateRateIndex = 3;
			unclickFindRebateRateAll();
			textFindRebateClassRate1120.setBackgroundColor(0xb8b8b8);
		}
	};
	
	OnClickListener textFindRebateClassRate21ClickListener = new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick || findRebateRateIndex == 4)
				return;
			findRebateRateIndex = 4;
			unclickFindRebateRateAll();
			l("textFindRebateClassRate21ClickListener clicked");
//			textFindRebateClassRate21.setBackgroundResource(R.color.find_rebate_rate_text_background);
			textFindRebateClassRate21.setBackgroundColor(0xb8b8b8);
			l("textFindRebateClassRate21ClickListener after clicked");
		}
	};
	
	private void unclickFindRebateRateAll(){
		textFindRebateClassAll.setBackgroundColor(Color.WHITE);
		textFindRebateClassRate05.setBackgroundColor(Color.WHITE);
		textFindRebateClassRate610.setBackgroundColor(Color.WHITE);
		textFindRebateClassRate1120.setBackgroundColor(Color.WHITE);
		textFindRebateClassRate21.setBackgroundColor(Color.WHITE);
	}
	
	
	static class ListNavSearchPromptAdapter extends BaseAdapter{
		
		static class ListNavSearchPromptAdapterDataItem{
			public ListNavSearchPromptAdapterDataItem(String n, boolean h){
				name = n;
				isHistory = h;
			}
			
			public String name;
			public boolean isHistory;
			public int vid;
		}
		
		private Context mContext;
		private ListNavSearchPromptAdapterDataItem[] mData;
		
		public ListNavSearchPromptAdapter(Context context, ListNavSearchPromptAdapterDataItem[] data){
			mContext = context;
			mData = data;
		}
		
		public int getCount() {
			return mData.length;
		}

		public Object getItem(int position) {
			return mData[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.list_search_prompt_item, null);
				convertView.setTag(R.id.imageListSearchPrompt, 
						convertView.findViewById(R.id.imageListSearchPrompt));
				convertView.setTag(R.id.textListSearchPrompt, 
						convertView.findViewById(R.id.textListSearchPrompt));		
			}
			
			String name = mData[position].name;
			boolean isHistory = mData[position].isHistory;
			
			((ImageView)convertView.getTag(R.id.imageListSearchPrompt)).setImageResource(
					isHistory ? R.drawable.ic_history : R.drawable.ic_search);
			((TextView)convertView.getTag(R.id.textListSearchPrompt)).setText(name);
			
			return convertView;
		}
	}
	
	private void switchNavViews(boolean enableNav){
		editNavSearch.setEnabled(enableNav);
		editInvisible.requestFocus();
		btnNavHotones.setEnabled(enableNav);
		btnNavFind.setEnabled(enableNav);
		btnNavRandomFind.setEnabled(enableNav);
		btnNavGetAll.setEnabled(enableNav);
		btnNavSubFood.setEnabled(enableNav);
		btnNavSubEntertain.setEnabled(enableNav);
		btnNavSubLife.setEnabled(enableNav);
		btnNavSubTravel.setEnabled(enableNav);
		btnNavSubShop.setEnabled(enableNav);
	}
	
	
	static String[] testString = new String[]{"餐饮美食", "休闲娱乐", "生活服务"};
	int now = 0;
	private void switchVendorClass(int direction){
		l("direction " + direction);
		if(direction > 0)
			now++;
		else
			now--;
		now = (now + 3) % 3;
		String nowString = testString[now];
		l("now is " + nowString + ", " + now);
	}
	
	//----------------------------------------------Don't panic
	
	private void unclickAllForAreaChoose(){
		textAreaClassArea.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassAllArea.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassWuhou.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassQingyang.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassChenghua.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassJinniu.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassJinjiang.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassGaoxin.setBackgroundResource(R.drawable.pc_pop_background_2);
		textAreaClassJinjiao.setBackgroundResource(R.drawable.pc_pop_background_2);
		
		textAreaClassArea.setTextColor(Color.BLACK);
		textAreaClassAllArea.setTextColor(Color.BLACK);
		textAreaClassWuhou.setTextColor(Color.BLACK);
		textAreaClassQingyang.setTextColor(Color.BLACK);
		textAreaClassChenghua.setTextColor(Color.BLACK);
		textAreaClassJinniu.setTextColor(Color.BLACK);
		textAreaClassJinjiang.setTextColor(Color.BLACK);
		textAreaClassGaoxin.setTextColor(Color.BLACK);
		textAreaClassJinjiao.setTextColor(Color.BLACK);
	}
	
	private void unclickAllForVendorClassChoose(){
		textVendorClassAll.setBackgroundResource(R.drawable.pc_pop_background_2);
		textVendorClassFood.setBackgroundResource(R.drawable.pc_pop_background_2);
		textVendorClassEntertain.setBackgroundResource(R.drawable.pc_pop_background_2);
		textVendorClassLife.setBackgroundResource(R.drawable.pc_pop_background_2);
		textVendorClassTravel.setBackgroundResource(R.drawable.pc_pop_background_2);
		textVendorClassShop.setBackgroundResource(R.drawable.pc_pop_background_2);
		
		textVendorClassAll.setTextColor(Color.BLACK);
		textVendorClassFood.setTextColor(Color.BLACK);
		textVendorClassEntertain.setTextColor(Color.BLACK);
		textVendorClassLife.setTextColor(Color.BLACK);
		textVendorClassTravel.setTextColor(Color.BLACK);
		textVendorClassShop.setTextColor(Color.BLACK);
	}
	
	OnClickListener textAreaClassAreaClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassArea.setBackgroundResource(R.drawable.pc_pop_background);
			textAreaClassArea.setTextColor(Color.WHITE);
			textAreaItem1.setEnabled(true);
			textAreaItem2.setEnabled(true);
			textAreaItem3.setEnabled(true);
			textAreaItem4.setEnabled(true);
			textAreaItem1.setText("附近500米");
			textAreaItem2.setText("附近1000米");
			textAreaItem3.setText("附近2000米");
			textAreaItem4.setText("全城");
		}
	};
	
	OnClickListener textAreaClassAllAreaClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassAllArea, "全部区域");
		}
	};
	
	OnClickListener textAreaClassWuhouClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassWuhou, "武侯区");
		}
	};
	
	OnClickListener textAreaClassQingyangClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassQingyang, "青羊区");
		}
	};
	
	OnClickListener textAreaClassChenghuaClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassChenghua, "成华区");
		}
	};
	
	OnClickListener textAreaClassJinniuClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassJinniu, "金牛区");
		}
	};
	
	OnClickListener textAreaClassJinjiangClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassJinjiang, "锦江区");
		}
	};
	
	OnClickListener textAreaClassGaoxinClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassGaoxin, "高新区");
		}
	};
	
	OnClickListener textAreaClassJinjiaoClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForAreaChoose();
			textAreaClassClickCommonPart(textAreaClassJinjiao, "近郊");
		}
	};
	
	OnClickListener textAreaItem1ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout4.setVisibility(View.GONE);
			switchViewsForFind(true);
			editFindChooseArea.setText("附近500米");
		}
	};
	
	OnClickListener textAreaItem2ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout4.setVisibility(View.GONE);
			switchViewsForFind(true);
			editFindChooseArea.setText("附近1000米");
		}
	};
	
	OnClickListener textAreaItem3ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout4.setVisibility(View.GONE);
			switchViewsForFind(true);
			editFindChooseArea.setText("附近2000米");
		}
	};
	
	OnClickListener textAreaItem4ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout4.setVisibility(View.GONE);
			switchViewsForFind(true);
			editFindChooseArea.setText("全城");
		}
	};
	
	OnClickListener textVendorClassAllClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassAll.setBackgroundResource(R.drawable.pc_pop_background);
			textVendorClassAll.setTextColor(Color.WHITE);
			textVendorItem1.setEnabled(true);
			textVendorItem2.setEnabled(true);
			textVendorItem3.setEnabled(true);
			textVendorItem4.setEnabled(true);
			textVendorItem5.setEnabled(true);
			textVendorItem1.setText("全部商户");
			textVendorItem2.setText("热门");
			textVendorItem3.setText("热门二级分类1");
			textVendorItem4.setText("热门二级分类2");
			textVendorItem5.setText("热门二级分类3");
		}
	};
	
	OnClickListener textVendorClassFoodClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassClickCommonPart(textVendorClassFood, "餐饮美食");
		}
	};
	
	OnClickListener textVendorClassEntertainClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassClickCommonPart(textVendorClassEntertain, "休闲娱乐");
		}
	};
	
	OnClickListener textVendorClassLifeClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassClickCommonPart(textVendorClassLife, "生活服务");
		}
	};
	
	OnClickListener textVendorClassTravelClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassClickCommonPart(textVendorClassTravel, "酒店旅游");
		}
	};
	
	OnClickListener textVendorClassShopClickListener = new OnClickListener() {
		public void onClick(View v) {
			unclickAllForVendorClassChoose();
			textVendorClassClickCommonPart(textVendorClassShop, "商场超市");
		}
	};
	
	OnClickListener textVendorItem1ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout5.setVisibility(View.GONE);
			textVendorItemClickCommonPart("全部商户");
			
		}
	};
	
	OnClickListener textVendorItem2ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout5.setVisibility(View.GONE);
			textVendorItemClickCommonPart("热门");
		}
	};
	
	OnClickListener textVendorItem3ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout5.setVisibility(View.GONE);
			textVendorItemClickCommonPart("热门二级分类1");
		}
	};
	
	OnClickListener textVendorItem4ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout5.setVisibility(View.GONE);
			textVendorItemClickCommonPart("热门二级分类2");
		}
	};
	
	OnClickListener textVendorItem5ClickListener = new OnClickListener() {
		public void onClick(View v) {
			linearLayout5.setVisibility(View.GONE);
			textVendorItemClickCommonPart("热门二级分类3");
		}
	};
	
	private void textAreaClassClickCommonPart(TextView v, String text){
		v.setBackgroundResource(R.drawable.pc_pop_background);
		v.setTextColor(Color.WHITE);
		textAreaItem1.setText("");
		textAreaItem2.setText("");
		textAreaItem3.setText("");
		textAreaItem4.setText("");
		textAreaItem1.setEnabled(false);
		textAreaItem2.setEnabled(false);
		textAreaItem3.setEnabled(false);
		textAreaItem4.setEnabled(false);
		
		linearLayout4.setVisibility(View.GONE);
		switchViewsForFind(true);
		editFindChooseArea.setText(text);
	}
	
	private void textVendorItemClickCommonPart(String text){
		switch(whoCalledVendorChoose){
			case 0:
				switchViewsForFind(true);
				editFindVendorClass.setText(text);
				break;
			case 1:
				switchViewsForRandomFind(true);
				if(text.equals("全部商户"))
					text = "不限商户类型";
				textRandomFindChooseVendor.setText(text);
				linearRandomFindBtnGroup.invalidate();
				break;
		}
	}
	
	private void textVendorClassClickCommonPart(TextView v, String text){
		v.setBackgroundResource(R.drawable.pc_pop_background);
		v.setTextColor(Color.WHITE);
		textVendorItem1.setText("");
		textVendorItem2.setText("");
		textVendorItem3.setText("");
		textVendorItem4.setText("");
		textVendorItem5.setText("");
		textVendorItem1.setEnabled(false);
		textVendorItem2.setEnabled(false);
		textVendorItem3.setEnabled(false);
		textVendorItem4.setEnabled(false);
		textVendorItem5.setEnabled(false);
		
		linearLayout5.setVisibility(View.GONE);
		
		switch(whoCalledVendorChoose){
			case 0:
				switchViewsForFind(true);
				editFindVendorClass.setText(text);
				break;
			case 1:
				switchViewsForRandomFind(true);
				textRandomFindChooseVendor.setText(text);
				break;
		}

	}
	
	OnClickListener imageMainHeaderShowNavClickListener = new OnClickListener() {
		public void onClick(View v) {
			final int x = ((AbsoluteLayout.LayoutParams)l2.getLayoutParams()).x;
			if(x == 0){
				switchNavViews(true);
				slideMain(x, 9, false);
			}else{
				switchNavViews(false);
				slideMain(x, 9, true);
			}
		}
	};
	
	
	static final int WORKING_AREA_STATE_PICS_MODE = 1;
	static final int WORKING_AREA_STATE_WALL_MODE = 2;
	static final int WORKING_AREA_STATE_FIND = 3;
	static final int WORKING_AREA_STATE_RANDOM_FIND = 4;
	static final int WORKING_AREA_STATE_GET_ALL = 5;
	static final int WORKING_AREA_STATE_MAP = 6;
	
	int WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_PICS_MODE;
	OnClickListener imageMainHeaderFuncClickListener = new OnClickListener() {
		public void onClick(View v) {
			switch(WORKING_AREA_STATE_CURRENT){
				case WORKING_AREA_STATE_PICS_MODE:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_WALL_MODE;
					hideAll();
					linearWallpaperMode.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_pics_mode);
					break;
				case WORKING_AREA_STATE_WALL_MODE:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_PICS_MODE;
					hideAll();
					linearPicMode.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_wallpaper_mode);
					break;
				case WORKING_AREA_STATE_FIND:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_RANDOM_FIND;
					hideAll();
					linearRandomFind.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_find);
					textMainHeaderTitle.setText("摇一摇");
					break;
				case WORKING_AREA_STATE_RANDOM_FIND:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_FIND;
					hideAll();
					linearFind.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_random_find);
					textMainHeaderTitle.setText("查找商户");
					break;
				case WORKING_AREA_STATE_GET_ALL:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_MAP;
					hideAll();
					linearGetAllMap.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_all_list);
					textMainHeaderTitle.setText("地图模式");
					break;
				case WORKING_AREA_STATE_MAP:
					WORKING_AREA_STATE_CURRENT = WORKING_AREA_STATE_GET_ALL;
					hideAll();
					linearGetAllList.setVisibility(View.VISIBLE);
					imageMainHeaderFunc.setImageResource(R.drawable.ic_header_all_map);
					textMainHeaderTitle.setText("全部商户");
					break;
			}
			
			
		}
	};
	
	private void hideAll(){
		linearPicMode.setVisibility(View.GONE);
		linearWallpaperMode.setVisibility(View.GONE);
		linearFind.setVisibility(View.GONE);
		linearRandomFind.setVisibility(View.GONE);
		linearGetAllList.setVisibility(View.GONE);
		linearGetAllMap.setVisibility(View.GONE);
	}
	
	private void slideMain(final int x, final int stride, final boolean toLeft){
		new Thread(){
			public void run(){
				for(int i = x;toLeft ? i > 0 : i < WIDTH;){
					if(toLeft){
						i -= stride;
					}else{
						i += stride;
					}
					Message msg = updateHandler.obtainMessage();
					if(i <= 0)i = 0;
					if(i >= WIDTH)i = WIDTH;
					msg.arg1 = i;
					updateHandler.sendMessage(msg);
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	
	int GESTURE_DIRECTION = 0;//-1 x, 1 y
	int INIT_DIRECTION = 0;
	boolean shouldPerformClick = true;
	int yFLYING = 0;
	int xPosition1 = -1, xPosition2, yPosition1, yPosition2;
	int sumDistance, xDistance, yDistance = 0;
	
	View.OnTouchListener mainCommonTouchListener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		    imm.hideSoftInputFromWindow(editNavSearch.getWindowToken(), 0);
		    editInvisible.requestFocus();
			
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				xPosition1 = (int) event.getRawX();
				shouldPerformClick = true;
			}
			
		    detector.onTouchEvent(event);
		    l("d is " + GESTURE_DIRECTION);
		    
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				if(INIT_DIRECTION == 0 && GESTURE_DIRECTION != 0){
					INIT_DIRECTION = GESTURE_DIRECTION;
				}
				if(INIT_DIRECTION == -1){
					xPosition2 = (int) event.getRawX();
					xDistance = xPosition2 - xPosition1;
					xPosition1 = xPosition2;
					
					int tempX = ((AbsoluteLayout.LayoutParams)l2.getLayoutParams()).x;
					tempX += xDistance;
					
					if(tempX >= WIDTH){
						tempX = WIDTH;
						switchNavViews(true);
					}
					if(tempX <= 0){
						tempX = 0;
						switchNavViews(false);
					}
					l2.setLayoutParams(new AbsoluteLayout.LayoutParams(
							AbsoluteLayout.LayoutParams.MATCH_PARENT, 
							AbsoluteLayout.LayoutParams.MATCH_PARENT, 
							tempX, 0));
				}
			}
		    
			if(event.getAction() == MotionEvent.ACTION_UP){
				slideIfNecessary();
				if(INIT_DIRECTION == 0){
					shouldPerformClick = true;
				}else{
					shouldPerformClick = false;
				}
				if(INIT_DIRECTION == 1 && yFLYING != 0){
					l("y flying!");
					//TODO go get y action
					switchVendorClass(yFLYING);
				}
				GESTURE_DIRECTION = INIT_DIRECTION = 0;
				yFLYING = 0;
			}
			return false;
		}
	};
	
	
	boolean navShown = false;
	
	private void slideIfNecessary(){
		final int x = ((AbsoluteLayout.LayoutParams)l2.getLayoutParams()).x;
		if(x >= (WIDTH / 2)){
			editNavSearch.setEnabled(true);
			slideMain(x, 5, false);
		}else{
			editNavSearch.setEnabled(false);
			slideMain(x, 5, true);
		}
	}
	
	Handler updateHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.arg1 == 0){
				switchNavViews(false);
			}
			if(msg.arg1 == WIDTH){
				switchNavViews(true);
			}
			l2.setLayoutParams(new AbsoluteLayout.LayoutParams(
			AbsoluteLayout.LayoutParams.MATCH_PARENT, 
			AbsoluteLayout.LayoutParams.MATCH_PARENT, 
			msg.arg1, 0));
			
		}
	};
	
    GestureDetector detector = new GestureDetector(new OnGestureListener() {
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}
		
		public void onShowPress(MotionEvent e) {
			
		}
		
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			if(Math.abs(distanceX) >= Math.abs(distanceY)){
				GESTURE_DIRECTION = -1;
			}else{
				GESTURE_DIRECTION = 1;
			}
			return false;
		}
		
		public void onLongPress(MotionEvent e) {
			
		}
		
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(Math.abs(velocityY) > Math.abs(velocityX)){
				yFLYING = velocityY > 0 ? 1 : -1;
			}
			else
				yFLYING = 0;
			return false;
		}
		
		public boolean onDown(MotionEvent e) {
			return false;
		}
	});
    
    private void makeItRight(View v){
		v.setOnTouchListener(mainCommonTouchListener);
		v.setOnClickListener(dummyClickListener);
    }
    
	View.OnClickListener dummyClickListener =  new OnClickListener() {
		public void onClick(View v) {
			if(!shouldPerformClick)
				return;
			l("dummyClickListener onClick, do nothing");
		}
	};
	
	void l(String s){
		android.util.Log.d("test", s);
	}

}



